<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>库存查询</title>
<%@ include file="/WEB-INF/pages/common/frameCommonJs.jsp"  %>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/datagrid-detailview.js"></script>
<script>
   var getClassUrl = "<%=request.getContextPath()%>/product/getAllPoductClassJson.action";
   var pcId = 0;
   $(function(){
	   $('.icon-reload').click(
				function (){ 
					$('#ocTree').tree({url:'<%=request.getContextPath()%>/product/getAllPoductClassJson.action'});
					$('#ocTree').tree('reload')
				}
			);
			$(".icon-reload").css("cursor","pointer");
			
		$('#ocTree').tree({
			animate: true,
			url: getClassUrl,
			onBeforeExpand:function(node){
				$('#ocTree').tree("options").url=getClassUrl + "?pc.id=" + node.id;
			},
			onClick:function(node){
				var node = $('#ocTree').tree('getSelected');
				$('#orgs').datagrid('reload',{pcId:node.id});
				$('#orgs').datagrid({pageNumber:1});
				pcId = node.id;
				$('#pcId').val(node.id);
				$('#ocTree').tree('expand',node.target);
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#ocTree').tree('select', node.target);
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
			
		});
		// 右侧表单内容
		$('#orgs').datagrid({
				fit:true,
				view: detailview,
				nowrap: false,
				striped: true,
				collapsible:true,
				url:'<%=request.getContextPath()%>/searchcenter/searchStockByPcId.action',
				sortName: 'proNo',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				resizable:true,
				title:'统计结果',
				detailFormatter:function(index,row){  
                    return '<div style="padding:5px"><table id="ddv-' + index + '"></table></div>';  
                },
                onExpandRow: function(index,row){
                  var stime = $("input[name=stime]").val();
   				  var etime = $("input[name=etime]").val();
                  $('#ddv-'+index).datagrid({ 
                    	doSize:true,  
                		cache:false, 
                        url:"searchStockByPro.action?pid="+row.pid+"&searchVoucherCondition.stime="+stime+"&searchVoucherCondition.etime="+etime,  
                        fitColumns:true,  
                        singleSelect:true,  
                        rownumbers:true,  
                        loadMsg:'读取中，请稍等...',  
                        height:'auto',  
                        columns:[[  
                            {field:'warehouse',title:'仓库',align:'left',width:100},  
                            {field:'initStock',title:'初始库存',align:'right',width:100},
                            {field:'purchaseNum',title:'进货数量',align:'right',width:100},  
                            {field:'rPurchaseNum',title:'进货退货数量',align:'right',width:100},
                            {field:'saleNum',title:'销售数量',align:'right',width:100},
                            {field:'rSaleNum',title:'销售退货数量',align:'right',width:100},
                            {field:'stock',title:'库存余量',align:'right',width:100,
                            	styler:function(value,row,index){
									if (value < 10){
										return 'background-color:#ffee00;color:red;font-weight:bold';
									}else{
										return 'font-weight:bold';
									}
								}
                            }
                        ]],  
                        onResize:function(){  
                            $('#orgs').datagrid('fixDetailRowHeight',index);  
                        },  
                        onLoadSuccess:function(){  
                            setTimeout(function(){  
                                $('#orgs').datagrid('fixDetailRowHeight',index);  
                            },0);  
                        },
                       	rowStyler: function(index,row){
							if (true){
								return 'background-color:#FFE4E1';
							}
						}
                    });  
                    $('#orgs').datagrid('fixDetailRowHeight',index);  
                },
                frozenColumns:[[
	                {field:'proNo',title:'编号',width:100},
					{field:'proName',title:'名称',width:150},
					{field:'proClassName',title:'类别',width:80},
					{field:'spec',title:'规格',width:50}
				]],
				columns:[[
							{title:'日常进货',colspan:2},
                            {title:'日常出货',colspan:2},
                            {title:'期末结存',colspan:1},
						 ],[
							/* {field:'unit',title:'单位',width:150}, */
							{field:'purchaseNum',title:'进货数量',width:100,align:'right',sortable:true},
							{field:'rPurchaseNum',title:'进货退货数量',width:100,align:'right',sortable:true},
							{field:'saleNum',title:'销售数量',width:100,align:'right',sortable:true},
							{field:'rSaleNum',title:'销售退货数量',width:100,align:'right',sortable:true},
							{field:'stock',title:'库存余量',width:100,align:'right',sortable:true,
								styler:function(value,row,index){
									if (value < 10){
										return 'background-color:#ffee00;color:red;font-weight:bold';
									}else{
										return 'font-weight:bold';
									}
								}
							}
				]],
				pageSize:15,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true
			});
   });
	function detail(id){
		var title = "详情";
		// 详细内容
   		$('#detailDlg').dialog('open').dialog('setTitle',title); 
   		$('#detailTable').datagrid('reload',{pid:id});
	}
	function searchClear(){
		location.reload()
	}
	//库存余量导出
	function exportRemainStock(){ 
		//getClassUrl = "<%=request.getContextPath()%>/searchcenter/exportRemainStock.action?pcId="+pcId;
	    //window.location.href = getClassUrl;
		getClassUrl = "<%=request.getContextPath()%>/searchcenter/exportRemainStock.action";
	    $('#searchForm').form('submit',{  
	        url:getClassUrl,  
	        onSubmit: function(){  
	        	var stime = $("#stime").val();
	        	var etime = $("#etime").val();
	        	var pcId = $("#pcId").val();
	        	var searchProName = $("#searchProName").val();
	            return $(this).form('validate');  
	        },  
	        success: function(result){  
	            var result = eval('('+result+')');
	            if (result==true){
	            	$.messager.show({title:"提示信息:", msg:"导出成功！"});
	            } else {  
	                $.messager.show({title:"提示信息:", msg:"导出失败！"});  
	            }  
	        }  
	    });  
	}
	
	function searchVoucher(){
		var node = $('#ocTree').tree('getSelected');
		var pcId;
		if(node!=null){
			pcId = node.id;
		}
		var searchProName = $("#searchProName").val();
		if($.trim(searchProName) != ""){
			pcId = "";
		}
		var stime = $("input[name=stime]").val();
   		var etime = $("input[name=etime]").val();
   		$("#sshowtime").html("<b>" + stime + "</b>");
   		$("#eshowtime").html("<b>" + etime + "</b>");
		$('#orgs').datagrid('reload',{'psdCondtion.pcId':pcId,'psdCondtion.stime':stime,'psdCondtion.etime':etime,'psdCondtion.searchProName':searchProName});
		$('#orgs').datagrid({pageNumber:1});
	}
</script>
</head>
<body class="easyui-layout" >
<div region="west" iconCls="icon-reload" title="商品分类" split="true" style="width:180px;">
		<ul id="ocTree"></ul>
</div>
<div region="center" title="库存余量" style="overflow:auto;">
	<div class="easyui-layout" data-options="fit:true">
		<div style="height:130px" data-options="region:'north',border:false">
			<div id="tb" class="datagrid-toolbar">  
			    <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="javascript:searchVoucher()">查询</a>  
			    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="javascript:searchClear()">重置</a>  
			    <a href="#" class="easyui-linkbutton" iconCls="icon-excel" plain="true" onclick="javascript:exportRemainStock()">导出</a>
			</div>   
			<center>
			<form id="searchForm" method="post">
			<table style="font-size:12px;padding:10px;" border="0">
				<input type="hidden" id="pcId" name="pcId" value="" />
				<tr>
					<td>
						商品名称 
					</td>
					<td align="left">
						<input type="text" name="searchProName" id="searchProName" title="查询所有商品" value="" />
					</td>
				</tr>
				<tr>
					<td align="right">
						统计日期
					</td>
					<td>
						<input class="easyui-datebox" name="stime" id="stime" style="width:100px" readonly="readonly" /> ~ <input class="easyui-datebox" name="etime" id="etime" style="width:100px" readonly="readonly" />
					</td>
				</tr>
			</table>
			</form>
			</center>
		</div>
		<div data-options="region:'center',border:false">
			<table id="orgs"></table>
		</div>
	</div>
</div>
</body>
</html>